package com.dlc.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.model.ProdExtension;
import com.dlc.shop.bean.vo.SkuStockVO;
import com.dlc.shop.common.constants.CacheNames;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.dao.ProdExtensionMapper;
import com.dlc.shop.service.ProdCommService;
import com.dlc.shop.service.ProdExtensionService;
import com.dlc.shop.service.SkuService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 *
 * @author LGH
 * @date 2022-05-05 10:47:48
 */
@Service
@AllArgsConstructor
public class ProdExtensionServiceImpl extends ServiceImpl<ProdExtensionMapper, ProdExtension> implements ProdExtensionService {

    private final ProdExtensionMapper prodExtensionMapper;

    private final SkuService skuService;
    private final ProdCommService prodCommService;

    @Override
    @Cacheable(cacheNames = CacheNames.PROD_EXTENSION_INFO_KEY, key = "#prodId")
    public ProdExtension getByProdId(Long prodId) {
        ProdExtension prodExtension = getOne(new LambdaQueryWrapper<ProdExtension>().eq(ProdExtension::getProdId, prodId));
        if (Objects.isNull(prodExtension)) {
            throw new YamiShopBindException("yami.product.no.exist");
        }
        return prodExtension;
    }

    @Override
    public int updateWaterSoldNum(Integer waterSaleNum, Long prodId) {
        return prodExtensionMapper.updateWaterSoldNum(waterSaleNum, prodId);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.PROD_EXTENSION_INFO_KEY, key = "#prodId")
    public void removeProdExtensionCache(Long prodId) {
    }

    @Override
    public Long getSoldNumByProdIds(List<Long> prodIdList) {
        return prodExtensionMapper.getSoldNumByProdIds(prodIdList);
    }

    @Override
    public void updateEsProdInfoToDb(List<Long> prodIds) {
        if (CollectionUtils.isEmpty(prodIds)) {
            return;
        }
        List<ProdExtension> prodExtensions = this.list(new LambdaQueryWrapper<ProdExtension>().in(ProdExtension::getProdId, prodIds));
        Map<Long, SkuStockVO> stockMap = skuService.mapProdStock(prodIds);
        Map<Long, Integer> commNumMap = prodCommService.listCommentNumByProdIds(prodIds);
        // 更新销量，库存，评论数
        for (ProdExtension prodExtension : prodExtensions) {
            SkuStockVO skuStockVO = stockMap.get(prodExtension.getProdId());
            prodExtension.setActualStock(Objects.isNull(skuStockVO) ? 0 : skuStockVO.getStock());
            prodExtension.setSoldNum(Objects.isNull(skuStockVO) ? 0 : skuStockVO.getSale());
            prodExtension.setCommNum(commNumMap.getOrDefault(prodExtension.getProdId(), 0));
        }
        // 批量更新
        this.updateBatchById(prodExtensions);
    }
}
